Skip to main content

XGUI 读取数据库向gameobject添加数据

using UnityEngine;
using System.Collections;
using System;
using Bitverse.Unity.Gui;
using Newtonsoft.Json.Linq;
using System.Text;

[ExecuteInEditMode]
public class BrowserHistoryForm : BitForm {

private string jsonURL="http://XXXXXXXXXXXXXXXXXXXXXXX"; //单条记录
private string jsonURLget = "http://XXXXXXXXXXXXXXXXXXXXXXX"; //查询多条记录
private string jsonURLsave = "http://XXXXXXXXXXXXXXXXXXXXXXX"; //保存单条记录
private string jsonURLdelete = "http://XXXXXXXXXXXXXXXXXXXXXXX"; //删除单条记录
public JArray shopcarArray;
public GameObject Group;
public GameObject prefab;
public ArrayList groupList = new ArrayList();//用来存储新产生的商品GameObject
public int _x = 0;
public int _y = 0;
public int _width = 300;
public int _height = 400; //全局变量不能写在函数里面。
public string goodsname ;
public string goodsprice ;
public string goodsid;
public string userid;

public void Start() {
StartCoroutine(DoGetMultitermData());
UpdateGrouplist(0);

}

public void OnGUI()
{
//~ if (_visible)
//~ {
BeforeOnGUI();
GUI.matrix = transform.localToWorldMatrix;
for (int i = 0, count = transform.childCount; i < count; i++)
{
Transform ch = transform.GetChild(i);
BitControl c = (BitControl)ch.GetComponent(typeof(BitControl));
c.Draw();
}
AfterOnGUI();
//~ }
}
protected virtual void BeforeOnGUI() { }
protected virtual void AfterOnGUI() {

if ((GameObject.Find("BrowserHistoryWindow/Button").GetComponent("BitButton") as BitButton).click) {
ClearGroup("1″);
}
//最多显示20个商品
if (groupList.Count == 21) {
print(groupList.Count);
//DeleteGroup(20,"1");
Transform ch = GameObject.Find("BrowserHistoryWindow/ScrollView").transform.GetChild(0);
string goodsid = ch.name;
groupList.RemoveAt(20);
StartCoroutine(DeleteData("1",goodsid));
Destroy(ch.gameObject);
}
}

//获取单条记录
public IEnumerator DoGetSingleData(string id) {
//获取json数据的方法
WWW getwww = new WWW (jsonURL + id);
yield return getwww;
string jsonObj1 = getwww.data; //单条json记录
JObject o = JObject.Parse(jsonObj1);
JArray sizes = (JArray)o["commodity"];
goodsname = (string)sizes[0]["name"];
goodsprice = (string)sizes[0]["price"];
goodsid = id;
(GameObject.Find("JoinCartForm/JoinCartWindow/TextArea1").GetComponent("BitTextArea") as BitTextArea).Text = goodsname;
(GameObject.Find("JoinCartForm/JoinCartWindow/TextField2").GetComponent("BitTextField") as BitTextField).Text = goodsprice;
StartCoroutine(SaveData("1",goodsid));
}

//初始化时获取多条记录
public IEnumerator DoGetMultitermData(){
//获取json数据的方法
WWW getwww = new WWW (jsonURLget);
yield return getwww;
string jsonObjget = getwww.data;
if(jsonObjget != "") {
JObject o = JObject.Parse(jsonObjget);
JArray sizes = (JArray)o["browserhistory"];
shopcarArray = sizes;
for(int i = 0;i<shopcarArray.Count;i++){
string incartName = (string)shopcarArray[i]["name"];
string incartPrice = (string)shopcarArray[i]["price"];
string incartGoodsid = (string)shopcarArray[i]["commodity_id"];
string incartUserid = (string)shopcarArray[i]["user_id"];
GameObject Group = NewGroup(incartUserid,incartGoodsid,incartName,incartPrice);
groupList.Add(Group);
UpdateGrouplist(0);
BitControl GroupScript = (BitControl)Group.GetComponent(typeof(BitGroup));
GroupScript.Position = new Rect(_x,_y+80*i,_width,_height);
}
}
}

//保存记录
public IEnumerator SaveData(string userid,string id){
//提交JSON数据的方法
Hashtable mydata=new Hashtable();
mydata.Add("user_id",userid);
mydata.Add("commodity_id",id);

//将数据转换为json字符串
string jsonstring=HashTable2Json(mydata);
WWWForm form = new WWWForm();
form.AddField("jsonstring", jsonstring);
WWW postwww = new WWW(jsonURLsave, form);
yield return postwww;
print (postwww.data);
if(postwww.data=="success"){
AddGroup(userid,goodsid,goodsname,goodsprice);
}else{
}
}
//删除记录
public IEnumerator DeleteData(string userid,string goodsid) {
WWW getwww = new WWW (jsonURLdelete +"user_id="+userid+"%20and%20commodity_id="+goodsid);
yield return getwww;
}

public IEnumerator DeleteData(string userid) {
WWW getwww = new WWW (jsonURLdelete +"user_id="+userid);
yield return getwww;
}

//~ 单条记录转换JSON数据格式
public static string HashTable2Json(Hashtable hashtable) {
if (hashtable.Count < 1 ){
return "{}";
}
//初始化
StringBuilder sb = new StringBuilder();
sb.Append(‘{‘);
foreach (System.Collections.DictionaryEntry objDE in hashtable) {
if (sb.Length > 1)
sb.Append(",");
sb.Append("\"" + objDE.Key.ToString() + "\":" + "\""+objDE.Value.ToString()+ "\"");
}

sb.Append(‘}’);
return sb.ToString();
}

public void AddGroup(string userid,string goodsid ,string goodsname,string goodsprice) {
GameObject Group = NewGroup(userid,goodsid,goodsname,goodsprice);
groupList.Insert(0,Group);//在top处插入
UpdateGrouplist(0);
}

public GameObject NewGroup(string userid,string goodsid ,string goodsname,string goodsprice) {
GameObject Group;
Group = (GameObject)Instantiate(prefab);
Group.transform.parent = transform.Find("BrowserHistoryWindow/ScrollView");
Group.name = goodsid;
// Group.transform.Find("GoodImages").GetComponent(BitDrawImage).Image = goodsimage;
(Group.transform.Find("GoodsName").GetComponent(typeof(BitLabel)) as BitLabel).Text = goodsname;
(Group.transform.Find("GoodsPrice").GetComponent(typeof(BitLabel)) as BitLabel).Text = goodsprice;
(Group.transform.Find("Close").GetComponent("CloseButton") as CloseButton).goodsid = goodsid;
(Group.transform.Find("Close").GetComponent("CloseButton") as CloseButton).userid = userid;
return Group;
}

//清空
public void ClearGroup(string userid) {
groupList.Clear();
Transform parentView = GameObject.Find("BrowserHistoryWindow/ScrollView").transform;
foreach (Transform bitgroup in parentView) {
Destroy(bitgroup.gameObject);
}
StartCoroutine(DeleteData(userid));
}

//遍历groupList并更新其坐标位置,以及设置相应的序号到其关闭按钮中
public void UpdateGrouplist(int startnum){
for(int k = startnum;k<groupList.Count;k++){ //用foreach效率极低!!!
//foreach(GameObject oneGroup in groupList) {
//(oneGroup.GetComponent(typeof(BitGroup)) as BitGroup).Position = new Rect(_x,_y+80*(k++),_width,_height);
(((GameObject)groupList[k]).GetComponent("BitGroup") as BitGroup).Position = new Rect(_x,_y+80*k,_width,_height);
(((GameObject)groupList[k]).transform.Find("Close").GetComponent("CloseButton") as CloseButton).listnum = k;
}
}
}